package fr.iut2.tc1.client;

import java.util.ArrayList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HasVerticalAlignment.VerticalAlignmentConstant;

import fr.iut2.tc1.shared.UnTroll;

public class ListeNom extends Composite implements ClickHandler
{
	private ScrollPanel page;
	private Label[] lesTroll;
	private ArrayList<Result> sauvegardeTroll;
	private String TrollFocus;
	private AbsolutePanel compo;
	
	public ListeNom(ArrayList<Result> result)
	{
		TrollFocus="";
		sauvegardeTroll=result;
		
		//Le composant
		page = new ScrollPanel();
		
		compo = new AbsolutePanel();
		page.add(compo);
		
		//On va afficher les noms
		lesTroll = new Label[sauvegardeTroll.size()];
		
		for(int i=0;i<result.size();i++)
		{
			lesTroll[i] = new Label(result.get(i).getName().split(".txt")[0]);
			lesTroll[i].setStyleName("choixTroll");
			lesTroll[i].addClickHandler(this);
			compo.add(lesTroll[i]);
		}
		
		
		
		//Initialisation du widget
		initWidget(page);
		
		// Style du widget
	    setStyleName("listeNom");
	}

	@Override
	public void onClick(ClickEvent event) 
	{
		for(int i=0;i<lesTroll.length;i++)
		{
			if(lesTroll[i]==event.getSource())
			{
				this.TrollFocus=this.sauvegardeTroll.get(i).getName();
				
				String infoTroll[] = this.sauvegardeTroll.get(i).getText().split("/");
				
				//On crée un Troll
				UnTroll trollChoisi = new UnTroll(this.sauvegardeTroll.get(i).getName().split(".txt")[0],infoTroll[0],this.sauvegardeTroll.get(i).getNomImage());
				trollChoisi.setPointsCompetence(Integer.parseInt(infoTroll[1]), Integer.parseInt(infoTroll[2]));
				trollChoisi.setCaracteristiques(Integer.parseInt(infoTroll[3]), Integer.parseInt(infoTroll[4]), Integer.parseInt(infoTroll[5]), Integer.parseInt(infoTroll[6]), Integer.parseInt(infoTroll[7]));
				
				
				ListeCaracteristiques.get().afficherDetail(trollChoisi);
				ListeTroll.get().getImageTroll().setUrl("image/"+trollChoisi.getNomImage());
				break;
			}
		}
		
	}
	
	public String getTrollFocus()
	{
		return this.TrollFocus;
	}
	
	public void setTri(String race)
	{
		//On va afficher les noms
		lesTroll = new Label[sauvegardeTroll.size()];
	
		page.remove(compo);
		compo = new AbsolutePanel();
		page.add(compo);
		
		//Ajout des nouvelles sauvegardes
		for(int i=0;i<sauvegardeTroll.size();i++)
		{
			if(sauvegardeTroll.get(i).getRace().equals(race) || race.equals("All"))
			{
				lesTroll[i] = new Label(sauvegardeTroll.get(i).getName().split(".txt")[0]);
				lesTroll[i].setStyleName("choixTroll");
				lesTroll[i].addClickHandler(this);
				compo.add(lesTroll[i]);
			}
			
		}
		
		
	}
}
